//
//  EFUsePointsViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/30.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFUsePointsViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "iToast.h"

@interface EFUsePointsViewController()<UITextFieldDelegate>

@property(strong,nonatomic)UILabel* totalPointTipsLabel;
@property(strong,nonatomic)UILabel* totalPointLabel;
@property(strong,nonatomic)UIView * totalPointLine;

@property(strong,nonatomic)UILabel* maxPointTipsLabel;
@property(strong,nonatomic)UILabel* maxPointLabel;
@property(strong,nonatomic)UIView * maxPointLine;


@property(strong,nonatomic)UIView * midView;
@property(strong,nonatomic)UILabel* midTipsLabel1;
@property(strong,nonatomic)UILabel* midTipsLabel2;
@property(strong,nonatomic)UITextField* pointsTF;
@property(strong,nonatomic)UILabel* totalSubLabel;

@property(strong,nonatomic)UIButton* okBtn;
@property(strong,nonatomic)UILabel* okTipsLabel;


@property (strong, nonatomic) TPKeyboardAvoidingScrollView * scrollView;


@property(strong,nonatomic)NSString* totalPrice;
@property(strong,nonatomic)NSString* couponId;
@property(strong,nonatomic)NSString* postage;
@property(strong,nonatomic)NSString* discount;


@property(strong,nonatomic)NSString* maxPoints;



@end

@implementation EFUsePointsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.title = @"积分抵扣";
    }
    return self;
}

-(instancetype)initWithTotalPirce:(NSString *)totalPrice couponId:(NSString *)couponId postage:(NSString *)postage discount:(NSString *)discount{
    self = [self init];
    if(self){
        self.totalPrice = totalPrice;
        self.couponId = couponId;
        self.postage = postage;
        self.discount = discount;
    }
    return self;
}

-(void)initView{
    [self.scrollView setBackgroundColor:[UIColor whiteColor]];
    [self.scrollView addSubview:self.totalPointTipsLabel];
    [self.scrollView addSubview:self.totalPointLabel];
    [self.scrollView addSubview:self.totalPointLine];
    [self.scrollView addSubview:self.maxPointTipsLabel];
    [self.scrollView addSubview:self.maxPointLabel];
    [self.scrollView addSubview:self.maxPointLine];
    [self.midView addSubview:self.midTipsLabel1];
    [self.midView addSubview:self.midTipsLabel2];
    [self.midView addSubview:self.pointsTF];
    [self.midView addSubview:self.totalSubLabel];
    [self.scrollView addSubview:self.midView];
    [self.scrollView addSubview:self.okBtn];
    [self.scrollView addSubview:self.okTipsLabel];
    [self.view addSubview:self.scrollView];
}

-(void)initData{
    [APP_CONTEXT.net getDiscountPoints:self.totalPrice couponId:self.couponId postage:self.postage discount:self.discount OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            if([info objectForKey:@"memberPoints"]){
                NSString* memberPoints = [NSString stringWithFormat:@"%@分",[info objectForKey:@"memberPoints"]];
                [self.totalPointLabel setText:memberPoints];
                [self.totalPointLabel sizeToFit];
                self.totalPointLabel.top = 15;
                self.totalPointLabel.left = kScreenWidth - self.totalPointLabel.width - 20;
            }
            if([info objectForKey:@"maxUseMemberPoints"]){
                self.maxPoints =[NSString stringWithFormat:@"%@",[info objectForKey:@"maxUseMemberPoints"]];
                NSString* maxUseMemberPoints =  [NSString stringWithFormat:@"%@分",[info objectForKey:@"maxUseMemberPoints"]];
                [self.maxPointLabel setText:maxUseMemberPoints];
                [self.maxPointLabel sizeToFit];
                self.maxPointLabel.top = self.totalPointLine.bottom + 15;
                self.maxPointLabel.left = kScreenWidth - self.maxPointLabel.width - 20;
            }
        }else{
            [[iToast makeText:message]show];
        }
    }];
}

-(TPKeyboardAvoidingScrollView*)scrollView{
    if(_scrollView == NULL){
        _scrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame: CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _scrollView.contentSize =  CGSizeMake(kScreenWidth, kScreenHeight - kStatusBarHeight);
    }
    return _scrollView;
}

-(UILabel *)totalPointTipsLabel{
    if(_totalPointTipsLabel == NULL){
        _totalPointTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
        _totalPointTipsLabel.font = [UIFont systemFontOfSize:13];
        _totalPointTipsLabel.textColor = kBlack333;
        _totalPointTipsLabel.left = 20;
        _totalPointTipsLabel.top = 15;
        [_totalPointTipsLabel setText:@"您当前拥有积分"];
    }
    return _totalPointTipsLabel;
}

-(UILabel *)totalPointLabel{
    if(_totalPointLabel == NULL){
        _totalPointLabel = [[UILabel alloc]init];
        _totalPointLabel.font = [UIFont systemFontOfSize:13];
        _totalPointLabel.textColor = kBlack333;
    }
    return _totalPointLabel;
}

-(UIView *)totalPointLine{
    if(_totalPointLine == NULL){
        _totalPointLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-40, 1)];
        _totalPointLine.backgroundColor = RGBCOLOR(0xECECEC);
        _totalPointLine.left = 20;
        _totalPointLine.top = self.totalPointTipsLabel.bottom + 15;
    }
    return _totalPointLine;
}


-(UILabel *)maxPointTipsLabel{
    if(_maxPointTipsLabel == NULL){
        _maxPointTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
        _maxPointTipsLabel.font = [UIFont systemFontOfSize:13];
        _maxPointTipsLabel.textColor = kBlack333;
        _maxPointTipsLabel.left = 20;
        _maxPointTipsLabel.top = self.totalPointLine.bottom + 15;
        [_maxPointTipsLabel setText:@"本订单最多可用"];
    }
    return _maxPointTipsLabel;
}

-(UILabel *)maxPointLabel{
    if(_maxPointLabel == NULL){
        _maxPointLabel = [[UILabel alloc]init];
        _maxPointLabel.font = [UIFont systemFontOfSize:13];
        _maxPointLabel.textColor = kBlack333;
    }
    return _maxPointLabel;
}

-(UIView *)maxPointLine{
    if(_maxPointLine == NULL){
        _maxPointLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-40, 1)];
        _maxPointLine.backgroundColor = RGBCOLOR(0xECECEC);
        _maxPointLine.left = 20;
        _maxPointLine.top = self.maxPointTipsLabel.bottom + 15;
    }
    return _maxPointLine;
}

-(UIView *)midView{
    if(_midView == NULL ){
        _midView = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 222)];
        _midView.backgroundColor = RGBCOLOR(0xF5F9F9);
        _midView.left = 20;
        _midView.top = self.maxPointLine.bottom + 40;
    }
    return _midView;
}


-(UILabel *)midTipsLabel1{
    if(_midTipsLabel1 == NULL){
        _midTipsLabel1 = [[UILabel alloc]init];
        _midTipsLabel1.font = [UIFont systemFontOfSize:13];
        _midTipsLabel1.textColor = kBlack333;
        [_midTipsLabel1 setText:@"输入本订单使用积分"];
        [_midTipsLabel1 sizeToFit];
        _midTipsLabel1.left = (self.midView.width - _midTipsLabel1.width)/2 ;
        _midTipsLabel1.top = 49;
    }
    return _midTipsLabel1;
}

-(UILabel *)midTipsLabel2{
    if(_midTipsLabel2 == NULL){
        _midTipsLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
        _midTipsLabel2.font = [UIFont systemFontOfSize:13];
        _midTipsLabel2.textColor = kBlack999;
        [_midTipsLabel2 setText:@"须为100的倍数"];
        [_midTipsLabel2 sizeToFit];
        _midTipsLabel2.left = (self.midView.width - _midTipsLabel2.width)/2 ;
        _midTipsLabel2.top = self.midTipsLabel1.bottom + 6;
    }
    return _midTipsLabel2;
}

-(UITextField *)pointsTF{
    if(_pointsTF == NULL){
        _pointsTF = [[UITextField alloc ]initWithFrame:CGRectMake(0, 0, 80, 32)];
        _pointsTF.left = (self.midView.width - _pointsTF.width)/2;
        _pointsTF.top = self.midTipsLabel2.bottom + 18;
        _pointsTF.font = [UIFont systemFontOfSize:15];
        _pointsTF.textColor = RGBCOLOR(0x69BA45);
        _pointsTF.delegate = self;
        _pointsTF.placeholder = @"请输入积分";
        
    }
    return _pointsTF;
}

-(UILabel *)totalSubLabel{
    if(_totalSubLabel == NULL){
        _totalSubLabel = [[UILabel alloc]init];
        _totalSubLabel.font = [UIFont systemFontOfSize:11];
        _totalSubLabel.textColor = kBlack999;
        [_totalSubLabel setText:@"抵扣xx"];
        [_totalSubLabel sizeToFit];
        _totalSubLabel.left = (self.midView.width - _totalSubLabel.width)/2 ;
        _totalSubLabel.top = self.pointsTF.bottom + 20;
    }
    return _totalSubLabel;
}

-(UIButton *)okBtn{
    if(_okBtn == NULL){
        _okBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth - 40, 40)];
        _okBtn.layer.cornerRadius = 4;
        [_okBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_okBtn setTitle:@"确定" forState:UIControlStateDisabled];
        _okBtn.left = 20;
        _okBtn.top = self.midView.bottom + 40;;
        [_okBtn setBackgroundColor:RGBCOLOR(0x69BA45)];
        [_okBtn addTarget:self action:@selector(onClickOkBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}

-(UILabel *)okTipsLabel{
    if(_okTipsLabel == NULL){
        _okTipsLabel = [[UILabel alloc]init];
        _okTipsLabel.font = [UIFont systemFontOfSize:11];
        _okTipsLabel.textColor = kBlack999;
        [_okTipsLabel setText:@"每200积分抵扣￥1.00"];
        [_okTipsLabel sizeToFit];
        _okTipsLabel.left = (kScreenWidth - _okTipsLabel.width)/2 ;
        _okTipsLabel.top = self.okBtn.bottom + 20;
    }
    return _okTipsLabel;
}

-(void)onClickOkBtn{
    NSString* points = self.pointsTF.text;
    if(points == nil  || points.length <= 0){
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    if(points.intValue >  self.maxPoints.intValue){
        [[iToast makeText:@"积分不能超过本单可用最大积分"]show];
        return;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:IC_CHANGE_POINTS_NOTIFICATION object:points];
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.pointsTF) {
        NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
        NSString* strSub = [NSString stringWithFormat:@"抵扣 %.2f",str.floatValue/200];
        [self.totalSubLabel setText:strSub];
        [self.totalSubLabel sizeToFit];
        self.totalSubLabel.left = (self.midView.width - self.totalSubLabel.width)/2;
        
    }
    return YES;
}


@end
